package top.mable.payment.config;


import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * 初始化rabbitmq的exchange、queue
 */
@Configuration
public class TTLRabbitMqConfig {

    /**
     * 声明注册direct模式的交换机
     * @return
     */
    @Bean
    public DirectExchange ttldirectExchange(){
        return new DirectExchange("ttl_direct_exchange",true,false);
    }

    /**
     * 设置队列，过期时间
     * 过期队列中过期的消息，以后会写入当死信队列中
     * @return
     */
    @Bean
    public Queue ttldirectQueue(){
        // 给队列设置过期时间
        Map args = new HashMap<>();
        args.put("x-message-ttl",5000); // 一定是int，毫秒

        //设置消息过期后，进入的死信队列
        args.put("x-dead-letter-exchange","dead_direct_exchange");
        args.put("x-dead-letter-routing-key","dead.key");
        return new Queue("ttl.direct.queue",true,false,false,args);
    }

    /**
     * 绑定
     * @return
     */
    @Bean
    public Binding ttldirectBinding(){
        return BindingBuilder.bind(ttldirectQueue()).to(ttldirectExchange()).with("ttl");
    }

    /*@Bean
    public Queue directMsgQueue(){

        return new Queue("ttl.msg.direct.queue",true,false,false);
    }*/


    /*@Bean
    public Binding ttlmsgdirectBinding(){
        return BindingBuilder.bind(directMsgQueue()).to(ttldirectExchange()).with("ttl.msg");
    }*/


}
